package com.stylefeng.guns.modular.vpn.utils;

import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.system.dao.ConnectionMapper;
import com.stylefeng.guns.modular.vpn.vo.ConnectionBO;
import com.stylefeng.guns.modular.vpn_connection_manage.service.IConnectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description TODO
 * @Author Mr.GodRui
 * @Date 2020-04-08 20:13
 **/
@Component
@Slf4j
public class ConnectUtil {
    @Autowired
    private IConnectionService connectionService;

    @Autowired
    private ConnectionMapper connectionMapper;

    /**
     * 断开所有连接
     */
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public void disAllConnections() throws Exception{
        //1、查询出所有连接时间超过24小时的设备连接对象
        List<ConnectionBO> connectionList =
                connectionMapper.queryMoreThanOneDayOfConnections();

        //2、循环断开每个连接
        if(connectionList != null && connectionList.size() > 0){
            //3、断开连接成功后，释放allowIp
            log.info("查询到超过24小时的连接数量： {}",connectionList.size());
            for (ConnectionBO connection :
                    connectionList) {
                connectionService.offLine(connection.getId().intValue());
                Thread.sleep(30);
            }
//            connectionList.forEach(c -> connectionService.offLine(c.getId().intValue()));
        }
    }
}
